import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_module/tool/shopDeviceManager.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:flutter_module/tabbar/shop/deviceModel.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:provider/provider.dart';
import 'shopScreenProvider.dart';
import 'shop_section_widget.dart';
import 'shop_row_widget.dart';
import 'shopModel.dart';
class ShopScreenCollection extends StatefulWidget {
  @override
  _ShopScreenCollectionState createState() => _ShopScreenCollectionState();
}

class _ShopScreenCollectionState extends State<ShopScreenCollection> with AutomaticKeepAliveClientMixin {

  @override
  bool get wantKeepAlive => true;

  EasyRefreshController _controller;

  List<DeviceModel> devices = [];

  @override
  void initState() { 
    super.initState();
    _controller = EasyRefreshController();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);

    return Consumer<ShopScreenProvider>(
      builder: (context,provider,child){
        return FutureBuilder(
          future: ShopDeviceManager.fetchCollectionShops(),
          builder: (context,snapshot){

            _controller.finishRefresh();

            if (!snapshot.hasData) {
              return Center(
                child: SpinKitCircle(color: Colors.lightBlue,),
              );
            }

            provider.setCollectionShops(snapshot.data);
            List<ShopSortModel> list = provider.collectionShopslist;
            if (list.length == 0) {
              return Center(
                child: Text('暂无收藏门店'),
              );
            }

            return EasyRefresh(
              enableControlFinishRefresh: true,
              controller: _controller,
              child: ListView.builder(
                physics: NeverScrollableScrollPhysics(),
                itemCount: list.length,
                itemBuilder: (context,section){
                  return Column(
                    children: <Widget>[
                      Container(
                        height: 30,
                        color: Color.fromRGBO(235, 239, 244, 1),
                        child: sectionTableViewSectionWidget(list[section]),
                      ),
                      ListView.builder(
                        physics: NeverScrollableScrollPhysics(),
                        shrinkWrap: true,
                        itemCount: list[section].shops.length,
                        itemBuilder: (context,row){
                          return sectionTableViewRowWidget(list[section].shops[row],provider);
                        },
                      )
                    ],
                  );
                },
              ),
              onRefresh: () async {
                await Future.delayed(Duration(seconds: 1),(){
                  provider.refreshCollectionShops();
                });
              },
            );
          },
        );
      },
    );
  }

  Widget sectionTableViewSectionWidget(ShopSortModel sortModel) {

    return ShopSectionWidget(sortModel.initial);
  }

  Widget sectionTableViewRowWidget(ShopModel model,ShopScreenProvider provider) {
    return ShopRowWidget(
      model: model,
      collectionOnTap:(){
        ShopDeviceManager.deleteCollectionShop(model.uuid).then((val){
          provider.collectionStautsChange(model.uuid);
          Fluttertoast.showToast(
            msg: '取消收藏成功',
            gravity: ToastGravity.CENTER
          );
        }).catchError((eror){
          Fluttertoast.showToast(
            msg: '取消收藏失败',
            gravity: ToastGravity.CENTER
          );
        });
      });
  }

}
